﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Transactions;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

namespace LCA.Data
{
    /// <summary>
    /// Summary description for Sequences
    /// </summary>
    public class Sequences
    {
        internal static int? GetNextVal(string sequenceName)
        {
            //Suppress transaction
            using (TransactionScope transactionScope = new TransactionScope(TransactionScopeOption.Suppress))
            {
                SqlConnection conn = null;

                try
                {
                    conn = new SqlConnection(ConfigurationManager.ConnectionStrings["c1"].ConnectionString);
                    conn.Open();

                    SqlCommand cmd = new SqlCommand("SequenceNextVal", conn);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add(new SqlParameter("@SeqName", sequenceName));
                    cmd.Parameters.Add(new SqlParameter("@SeqValue", SqlDbType.Int) { Direction = ParameterDirection.Output });

                    if (cmd.ExecuteNonQuery() > 0)
                    {
                        return (int)cmd.Parameters["@SeqValue"].Value;
                    }
                }
                finally
                {
                    if (conn != null)
                    {
                        conn.Close();
                    }

                    transactionScope.Complete();
                }
            }

            return null;
        }

        public static class FlowId
        {
            public static int? NextVal
            {
                get { return GetNextVal("FlowId"); }
            }
        }
        public static class LCASystemId
        {
            public static int? NextVal
            {
                get { return GetNextVal("LCASystemId"); }
            }
        }
    }
}